<?php
namespace admin;
use models\BaseDao;
use Slince\Upload\UploadHandlerBuilder;
class Ad extends Admin{
    public function  __construct(){
        parent::__construct();
        $this->assign('menumark','ad');
        $this->assign('allposition',
        ['1'=>'首页顶部广告(980*80)',
        '2'=>'首页底部广告(980*80)',
        '3'=>'所有页面顶部广告(980*80)',
        '4'=>'所有页面底部广告(980*80)',
        '5'=>'首页轮播图广告(730*300)']);
    }
/**
 * 广告列表页面
 */
    function index(){
        // 获取数据库操作对象
        $db = new BaseDao();
        // 获取全部广告,按照ord排序
        $data = $db->select('ad','*',['ORDER'=>['ord'=>'ASC','id'=>'DESC']]);
        // 将数据全部分给模板
        $this->assign('data',$data);

        //标题    
        $this->assign('title','广告列表');
        // 导入模板
        $this->display('ad/index');
    }
    function add(){
        // 如果$_POST['do_submit]存在,说明是添加动作
        if(isset($_POST['do_submit'])){



            
            // 上传图片
            $path = TEMPDIR."/uploads/ad";
            $builder = new UploadHandlerBuilder(); //create a builder.
            $handler = $builder
            //add constraints
            ->allowExtensions(['jpg','png','gif'])
            ->allowMimeTypes(['image/*'])

            ->saveTo($path) //save to local
            ->getHandler();

            $files = $handler->handle();
            $filename = $files['logo']->getUploadedFile()->getClientOriginalName(); // original name

            $newfilename = date('Y-md') . '-' . uniqid() . '.' . $files['logo']->getUploadedFile()->getClientOriginalExtension();

            rename($path.'/'.$filename,$path.'/'.$newfilename);






            $db = new BaseDao();
            unset($_POST['do_submit']);

            $_POST['logo'] = $newfilename;
            if($db->insert('ad',$_POST)){
                $this->success('/admin/ad','添加ok');
            }else{
                $this->error('/admin/ad/add','添加fail');
            }
        }
        //标题    
        $this->assign('title','添加广告');
        $this->display('ad/add');

    }
    function mod($id){
        $db = new BaseDao();
        $this->assign('ad',$db->get('ad','*',['id'=>$id]));
        $this->assign('title',"修改广告");
        $this->display('ad/mod');
    }
    function doupdate(){
        $id = $_POST['id'];
        unset($_POST['id']);

        if($_FILES['logo']['error']==0){
            // 修改图片
            $path = TEMPDIR."/uploads/ad";
            $builder = new UploadHandlerBuilder(); //create a builder.
            $handler = $builder
            //add constraints
            ->allowExtensions(['jpg','png','gif'])
            ->allowMimeTypes(['image/*'])

            ->saveTo($path) //save to local
            ->getHandler();

            $files = $handler->handle();
            $filename = $files['logo']->getUploadedFile()->getClientOriginalName(); // original name

            $newfilename = date('Y-md') . '-' . uniqid() . '.' . $files['logo']->getUploadedFile()->getClientOriginalExtension();

            rename($path.'/'.$filename,$path.'/'.$newfilename);
            $_POST['logo'] = $newfilename;
        }
        $db = new BaseDao();
        if($db->update('ad',$_POST,['id'=>$id])){
            $this->success('/admin/ad','修改ok');
        }else{
            $this->error('/admin/ad/mod/'.$id,'修改fail');
        }
    }
    function del($id){
        $db = new BaseDao();

        $logo = $db->get('ad','logo',['id'=>$id]);

        if($db->delete('ad',['id'=>$id])){
            // 删除图片
            $path = TEMPDIR."/uploads/ad";
            @unlink($path.'/'.$logo);

            $this->success('/admin/ad','删除ok');
        }else{
            $this->error('/admin/ad','删除fail');
        }
    }
    function order(){
        $db = new BaseDao();
        $num = 0;
        foreach($_POST['ord'] as $id=>$ord){
            
            $num += $db->update('ad',['ord'=>$ord],['id'=>$id]);
        }
        if($num > 0){
            $this->success('/admin/ad','排序ok');
        }else{
            $this->success('/admin/ad','排序fail');
        }
    }
}